from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from taggit.managers import TaggableManager

# Create your models here.

class Post(models.Model):
    #帖子状态选择
    STATUS_CHOICES = (
	('draft', '草稿'), 
	('publish', '公开'),
	('private', '私密'), )
    #帖子标题
    title = models.CharField(max_length=64)
    #帖子作者
    author = models.ForeignKey(
	User, related_name='blog_posts', )
    #帖子短标签
    slug = models.SlugField(max_length=64)
    #帖子标签
    tags = TaggableManager()
    #帖子主体内容
    content = models.TextField()
    #帖子浏览数
    view_count = models.IntegerField(default=0)
    #帖子评论数
    comment_count = models.IntegerField(default=0)
    #帖子创建时间
    created = models.DateTimeField(auto_now_add=True)
    #帖子修改更新时间
    updated = models.DateTimeField(auto_now=True)
    #帖子状态
    status = models.CharField(
    	max_length=10, 
    	choices=STATUS_CHOICES, 
    	default='draft',
    )

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.id,])

    class Meta:
    	ordering = ('-created',)

    def __str__(self):
    	return self.title

class Category(models.Model):
    #帖子版块分类
    name = models.CharField(max_length=23, unique=True)

    def __str__(self):
        return self.name

class Comment(models.Model):
    #评论的帖子
    post = models.ForeignKey(Post, related_name='comments')
    #评论用户名
    username = models.CharField(max_length=80)
    #评论者邮箱
    email = models.EmailField()
    #评论内容
    content = models.TextField()
    #评论时间
    created = models.DateTimeField(auto_now_add=True)
    #评论更新时间
    updated = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return 'Comment by {} on {}'.format(self.username, self.post)
